//
//  LeftView.m
//  ECBBusinessCamp
//
//  Created by rimi on 16/6/27.
//  Copyright © 2016年 邓毕华. All rights reserved.
//

#import "LeftView.h"

#import "SDWebImageCompat.h"

@interface LeftView ()

@property (nonatomic, strong) UIView * topView; // 头像与昵称
@property (nonatomic, strong) UIImageView * headImageView; // 头像
@property (nonatomic, strong) UILabel * nameLable; // 昵称
@property (nonatomic, strong) UIView * bottomView; // 按钮组
@property (nonatomic, assign) NSInteger selectButtonIndex; // 当前选中按钮下标

@end

@implementation LeftView

#pragma mark 🌷🌷🌷life cycle🌷🌷🌷
- (instancetype)initWithSelecBtnBlock:(SelecBtnBlock)selecBtnBlock;
{
    self = [super init];
    if (self) {
        _selecBtnBlock = selecBtnBlock;
        
        // 添加视图
        [self addSubview:self.topView];
        [self addSubview:self.bottomView];
        [_topView addSubview:self.headImageView];
        [_topView addSubview:self.nameLable];
        
        // 自动布局
        [_topView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(self);
            make.height.equalTo(self).multipliedBy(0.22);
            make.centerX.equalTo(self);
            make.top.offset(0);
        }];
        [_bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(_topView);
            make.top.equalTo(_topView.mas_bottom);
            make.centerX.equalTo(self);
            make.bottom.offset(0);
        }];
        [_headImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.height.equalTo(_topView.mas_width).multipliedBy(0.5);
            make.centerX.equalTo(_topView);
            make.centerY.equalTo(_topView).multipliedBy(0.95);
        }];
        [_nameLable mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(_topView);
            make.top.equalTo(_headImageView.mas_bottom);
        }];
        
        // 默认第一个按钮处于选中状态
        UIButton * firstButton = [_bottomView viewWithTag:10];
        firstButton.selected = YES;
        _selectButtonIndex = 10;
        firstButton.userInteractionEnabled = NO;
    }
    return self;
}

#pragma mark 🌷🌷🌷event responds🌷🌷🌷
- (void)respondsToSelectButton:(UIButton *)sender {
    UIButton * beforeButton = [_bottomView viewWithTag:_selectButtonIndex];
    beforeButton.selected = NO;
    beforeButton.userInteractionEnabled = YES;
    sender.selected = YES;
    sender.userInteractionEnabled = NO;
    _selectButtonIndex = sender.tag;
    _selecBtnBlock(_selectButtonIndex - 10);
}

#pragma mark 🌷🌷🌷getters and setters🌷🌷🌷
- (UIView *)topView {
    if (!_topView) {
        _topView = [[UIView alloc] init];
        _topView.backgroundColor = RGB_COLOR(180, 186, 189, 1);
    }
    return _topView;
}
- (UIView *)bottomView {
    if (!_bottomView) {
        _bottomView = [[UIView alloc] init];
        
        // 按钮名称数组
        NSArray * buttonNames = @[@"我的课程", @"我的笔记", @"我的收藏", @"我的问答", @"系统消息", @"学友管理", @"我的活动", @"我的帖子", @"我的下载", @"设置"];
        
        // 创建按钮组
        for (NSInteger i = 0; i < buttonNames.count; i++) {
            UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.tag = i + 10;
            button.titleLabel.font = [UIFont systemFontOfSize:23];
            button.adjustsImageWhenHighlighted = NO;
            [button addTarget:self action:@selector(respondsToSelectButton:) forControlEvents:UIControlEventTouchUpInside];
            [button setTitle:buttonNames[i] forState:UIControlStateNormal];
            [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [button setTitleColor:RGB_COLOR(0, 53, 145, 1) forState:UIControlStateSelected];
            [button setBackgroundImage:[UIImage imageNamed:@"i_up_选中框_02_normal.png"] forState:UIControlStateNormal];
            [button setBackgroundImage:[UIImage imageNamed:@"i_up_选中框_02.png"] forState:UIControlStateSelected];
            
            // 取到前一个按钮
            UIButton * beforeButton = [_bottomView viewWithTag:button.tag - 1];
            
            // 添加按钮
            [_bottomView addSubview:button];
            
            // 自动布局
            [button mas_makeConstraints:^(MASConstraintMaker *make) {
                make.width.equalTo(_bottomView);
                make.height.equalTo(_bottomView).multipliedBy(0.1);
                make.centerX.equalTo(_bottomView);
                make.top.equalTo(i == 0 ? _bottomView : beforeButton.mas_bottom);
            }];
        }
    }
    return _bottomView;
}
- (UIImageView *)headImageView {
    if (!_headImageView) {
        _headImageView = [[UIImageView alloc] init];
        [_headImageView sd_setImageWithURL:[NSURL URLWithString:BASEURLAPPEND([LoginUserInfo defaultUserInfo].head)] placeholderImage:[UIImage imageNamed:@"default_head.png"]];
        _headImageView.layer.borderColor = RGB_COLOR(0, 230, 255, 1).CGColor;
        _headImageView.layer.borderWidth = 2;
    }
    return _headImageView;
}
- (UILabel *)nameLable {
    if (!_nameLable) {
        _nameLable = [[UILabel alloc] init];
        _nameLable.textColor = [UIColor whiteColor];
        _nameLable.font = [UIFont systemFontOfSize:25];
        _nameLable.text = [LoginUserInfo defaultUserInfo].nikename;
    }
    return _nameLable;
}

@end
